package edu.ucsd.ccdb.rightClickPopupUtil;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JComponent;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

import edu.ucsd.irods.IRODSFileString;
import edu.ucsd.irods.IRODSOperation;
import edu.ucsd.irods.SQLDBQuery;

public class CutAndPasteActionListener implements ActionListener {

	private JPopupMenu menu;
	private ClipboardManager clipboard;
	private JTree tree;
	
	public CutAndPasteActionListener (JPopupMenu menu, JTree tree) {
		this.menu = menu;
		clipboard = new ClipboardManager();
		this.tree = tree;
	}
	
	public void actionPerformed(ActionEvent e) {
		
		JMenuItem menuItem = (JMenuItem) e.getSource();
		
		if ( menuItem.getText().equalsIgnoreCase("Cut")) {
			JComponent pasteComponent = (JComponent) menu.getComponent(5);
			pasteComponent.setEnabled(true);
			
			// add this to clipboard
			TreePath selectedPath = tree.getSelectionPath();
			DefaultMutableTreeNode node = (DefaultMutableTreeNode)  selectedPath.getLastPathComponent() ;
			
			clipboard.addFile( node);
		}
		else if (menuItem.getText().equalsIgnoreCase("Paste")) {
			
			DefaultMutableTreeNode fileFrom = clipboard.getLastFile();
			
			TreePath selectedPath = tree.getSelectionPath();
			DefaultMutableTreeNode fileTo = (DefaultMutableTreeNode) selectedPath.getLastPathComponent();

			// validation	
			if ( ((IRODSFileString) fileTo.getUserObject()).isDirectory() ) { 
			
				// do animation
				DefaultTreeModel model = (DefaultTreeModel)tree.getModel();
				
				model.removeNodeFromParent( fileFrom );
				// put the node back
				model.insertNodeInto( fileFrom , fileTo , fileTo.getChildCount() ); // add to the end of the folder

				
				// get the action on the back-end				
				IRODSFileString sourceFile = (IRODSFileString) fileFrom.getUserObject();
				String destinationFile = ((IRODSFileString) fileTo.getUserObject()).getAbsolutePath() + "/" + sourceFile.getName() ;

				try {
					IRODSOperation.moveFile(sourceFile.getAbsolutePath() , destinationFile);
				} catch (NullPointerException e1) {
					e1.printStackTrace();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
				
				// update the database to reflect the change 
				SQLDBQuery.manageCutAndPasteNameChange(sourceFile.getAbsolutePath(), destinationFile, sourceFile.isDirectory());
				
				// change the metadata on the node
				IRODSFileString file = (IRODSFileString) fileFrom.getUserObject();
				file.setAbsolutePath( ((IRODSFileString) fileTo.getUserObject()).getAbsolutePath() + "/" + sourceFile.getName() ) ;
				System.out.println ("Path after paste: " +  file.getAbsolutePath());
								
			}
			else {
				System.out.println ("Select a file to paste. Exception thrown");
				
				//TODO throws exception or popup warning 
				
			}
			
		}
	}

}
